package com.zz.yt.test.delegate.net;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.net.callback.IError;
import com.whf.android.jar.net.callback.ISuccess;
import com.whf.android.jar.net.utils.RestClientUtils;
import com.zz.yt.test.R;
import com.zz.yt.test.net.NextRetrofit;

import java.util.WeakHashMap;


/**
 * 新旧 RetrofitT 请求
 *
 * @author qf
 * @version 1.0.2
 */
public class MapActivity extends Activity implements IError {

    private final static String URL_0 = "http://1.203.97.36:19090/user/login";
    private final static String URL_1 = "https://chxht.shzgx.com/user/login";
    private final static String URL = "https://zzz.shzgx.com/user/login";

    String map;

    // 字体大小不跟随系统
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getConfigurationContext(newBase));
    }

    // 字体大小不跟随系统
    private static Context getConfigurationContext(@NonNull Context context) {
        Configuration configuration = context.getResources().getConfiguration();
        configuration.fontScale = 1f;
        return context.createConfigurationContext(configuration);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
    }

    @NonNull
    private WeakHashMap<String, Object> params() {
        WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("username", "汪海峰");
        params.put("password", "whf123456");
        params.put("grant_type", "password");
        params.put("scope", "server");
        return params;
    }

    public void onClick1(View view) {
        //RxRetrofit
        NextRetrofit.init(this).post(URL, params(), new ISuccess() {
            @Override
            public void onSuccess(String response) {
                map = response;
                ToastUtils.showShort(response);
                setText();
            }
        });
    }

    public void onClick2(View view) {
        NextRetrofit.init(this).getTaKen(URL, params(), new ISuccess() {
            @Override
            public void onSuccess(String response) {
                ToastUtils.showShort(response);
                map = response;
                setText();
            }
        });
    }

    public void onClick3(View view) {
        RestClientUtils.create().inPost(URL_0, params(), new ISuccess() {
            @Override
            public void onSuccess(String response) {
                ToastUtils.showShort(response);
                map = response;
                setText();
            }
        });
    }

    public void onClick4(View view) {
        RestClientUtils.create().inPost(URL_1, params(), new ISuccess() {
            @Override
            public void onSuccess(String response) {
                ToastUtils.showShort(response);
                map = response;
                setText();
            }
        });
    }

    public void onClick5(View view) {
        RestClientUtils.create().inPost(URL, params(), new ISuccess() {
            @Override
            public void onSuccess(String response) {
                ToastUtils.showShort(response);
                map = response;
                setText();
            }
        });
    }

    private void setText() {
        setText(true);
    }

    private void setText(boolean is) {
        TextView textView = findViewById(R.id.text);
        if (textView != null) {
            textView.setText(map);
            textView.setTextColor(ColorUtils.getColor(is ? R.color.colorAccent : R.color.textColor_45));
        }
    }

    @Override
    public void onError(int code, String msg) {
        map = msg;
        ToastUtils.showShort(msg);
        setText(false);
    }


}
